package com.yf.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.yf.mp.domain.User;
import com.yf.mp.dto.PageDTO;
import com.yf.mp.dto.UserDTO;
import com.yf.mp.query.UserQuery;
import com.yf.mp.service.UserService;
import com.yf.mp.mapper.UserMapper;
import com.yf.mp.vo.UserVO;
import org.apache.logging.log4j.message.ObjectMessage;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
* @author 62493
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-11-17 17:54:37
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public List<User> getUsersByIds(List<Long> ids) {
        return this.lambdaQuery().in((null!=ids && ids.size()>0),User::getId,ids).list();
    }

    @Override
    public User getUsersByIds(String name) {
        return this.lambdaQuery().like(null!=name,User::getUsername,name).orderByDesc(User::getCreateTime).list().stream().findFirst().orElse(null);
    }

    @Override
    public long getCount(String name) {
        return this.lambdaQuery().like(null!=name,User::getUsername,name).orderByDesc(User::getCreateTime).count();
    }

    @Override
    public boolean updateBalanceById(Long id, Integer money) {
        User user = this.getById(id);
        // 2.校验用户状态
        if (user == null || user.getStatus().getValue() == 2) {
            throw new RuntimeException("用户状态异常！");
        }
        // 3.校验余额是否充足
        if (user.getBalance() < money) {
            throw new RuntimeException("用户余额不足！");
        }

        Integer remainBalance = user.getBalance() - money;

        boolean update = this.lambdaUpdate()
                .set(User::getBalance, remainBalance)
                .set(remainBalance <= 0, User::getStatus, 2)
                .eq(User::getId, id)
                .eq(User::getBalance, user.getBalance())
                .update();
        return update;
    }

    @Override
    public List<UserDTO> getUserList() {
        QueryWrapper<User> queryUserWrapper = this.query().getWrapper().in("u.id", List.of(1L, 2L, 4L))
                .eq("a.city", "北京");
        return baseMapper.queryUserByWrapper(queryUserWrapper);
    }

    @Override
    public PageDTO<UserVO> queryUsersPage(UserQuery query) {
        Page<User> pageParam = query.toMpPageDefaultSortByCreateTimeDesc();
        Page<User> page = lambdaQuery().like(StrUtil.isNotBlank(query.getName()), User::getUsername, query.getName())
                .eq(ObjectUtil.isNotNull(query.getStatus()), User::getStatus, query.getStatus())
                .gt(ObjectUtil.isNotNull(query.getMinBalance()), User::getBalance, query.getMinBalance())
                .lt(ObjectUtil.isNotNull(query.getMaxBalance()), User::getBalance, query.getMaxBalance())
                .page(pageParam);
        return PageDTO.of(page,UserVO.class);
    }

    @Override
    public PageDTO<UserVO> queryCustomUsersPage(UserQuery query) {
        Page<User> page = query.toMpPageDefaultSortByCreateTimeDesc();
        page = lambdaQuery().like(StrUtil.isNotBlank(query.getName()), User::getUsername, query.getName())
                .eq(ObjectUtil.isNotNull(query.getStatus()), User::getStatus, query.getStatus())
                .gt(ObjectUtil.isNotNull(query.getMinBalance()), User::getBalance, query.getMinBalance())
                .lt(ObjectUtil.isNotNull(query.getMaxBalance()), User::getBalance, query.getMaxBalance())
                .page(page);
        return PageDTO.of(page, user -> {
            // 拷贝属性到VO
            UserVO vo = BeanUtil.copyProperties(user, UserVO.class);
            // 用户名脱敏
            String username = vo.getUsername();
            vo.setUsername(username.substring(0, username.length() - 2) + "**");
            return vo;
        });
    }
}




